package com.jumi.microservice.textMessages.util;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.jumi.microservice.textMessages.entity.JiGuangResponse;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Security;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class JiGuangUtil {

    private static String auth="Basic MGMxOTg0OTNkNjRjNDg3N2RhYmVjODg1OmQ3YjY0MDRkMWRmMmIyMGQ0NWFiZjVhZg==";


    private static String privateKey="MIICXAIBAAKBgQCtUxJ3nzWN8BQooFVvTymeuhlUELgsR6UGVb/Wl3kvlmGpca9wiQHF8vu9aDfPrjcliFyvLwsK5OvIDMcykCM4Vl7/UdoaLJpXWjmGjGGL+TFnkSLlvVGQ2A1voYRlle3s3DMfBNa8/kAjcPD0aQSWsFRQSg0nw13/2zdv0+vI+wIDAQABAoGAE8Y3hChI3uG2GxB3ztQ0RmSrARlLOdoOrNNZqKztaY15pPBQeMFYz3GyYOCkBXC13/VXxbDTTLHjaJZ0n4MsUVAPQ6vr1MJlZqyB4jmh01MysDdNEuwA39e+PTfBtbn0rdM2j3UgKNA5yYIfa6lSbIizuHv4ZYQSgn5ykMN7FzECQQDV7NXFdbMsWtjxB9Ha6bHXp39GESwE3nTUKz0k+AJcqzn45tE4SPiz3fVF/ChqDUcQNVLfBecP8bl3 sDDDxnR5AkEAz2n9EQOn3/SuPrg0Agd7vDFaV2takc0yHKtZR1gxHDZqK1tG/xGEmBV+3uGP82P9Cc5aLiga2fl0VozJJQlEEwJAJ29GJOVChy2kCiKH49Pqjbd5tb/anncnkCj8V4FEZjLGOATMkpSZDAchfKo0BQC3ldxOj4A2DPFhYBfhlWVS+QJABu8lLb9wilU0yhYAbxzu7xC7U3bcVUmF5l14I6F4KthK3l4z1m0wgKEfV7Y0yM9L81r5utdOZoEneYlFZD6WDwJBAK0lux7TPEay+53cKV+wd8qudVgjdr3KComwAOgGtROH9TNhgWGc/ezyITVGheToQeCcdStJmLi0B5x7P133Lhw=";


    public static JiGuangResponse verify(String token, String phone){
        Map paramMap=new HashMap();
        paramMap.put("token",token);
        paramMap.put("phone",phone);
        //链式构建请求
        String result = HttpRequest.post("https://api.verification.jpush.cn/v1/web/h5/verify")
                .header(Header.AUTHORIZATION, auth)//头信息，多个头信息多次调用此方法即可
                .header(Header.CONTENT_TYPE,"application/json")
                .body(JSONObject.toJSONString(paramMap))//表单内容
                .execute().body();
        JSONObject parse = (JSONObject) JSONObject.parse(result);

        JiGuangResponse reponse=new JiGuangResponse(
                parse.getString("id"),
                parse.getString("exId"),
                parse.getString("code"),
                parse.getString("content"),
                "");
        return reponse;
    }


    public static JiGuangResponse loginTokenVerify(String loginToken){
        Map paramMap=new HashMap();
        paramMap.put("loginToken",loginToken);
        //链式构建请求
        String result = HttpRequest.post("https://api.verification.jpush.cn/v1/web/loginTokenVerify")
                .header(Header.AUTHORIZATION, auth)//头信息，多个头信息多次调用此方法即可
                .header(Header.CONTENT_TYPE,"application/json")
                .body(JSONObject.toJSONString(paramMap))//表单内容
                .execute().body();
        JSONObject parse = (JSONObject) JSONObject.parse(result);

        JiGuangResponse reponse=new JiGuangResponse(
                parse.getString("id"),
                parse.getString("exId"),
                parse.getString("code"),
                parse.getString("content"),
                parse.getString("phone"));
        return reponse;
    }

    public static String decode(String encodePhone) {
        String result = null;
        try {
            result = decrypt(encodePhone,privateKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
    }

    public static String decrypt(String cryptograph, String prikey) throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getMimeDecoder().decode(prikey));
        PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);

        Cipher cipher=Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        byte [] b = Base64.getDecoder().decode(cryptograph);
        return new String(cipher.doFinal(b));
    }
}
